package com.example.tossednfound;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

/**
 * 
 *
 */
public class Item {

	public String creator;
	public String photo;
	public int rewardAmt;
	public String category;
	public String state;
	public String zipCode;
	public String streetAddress;
	public String country;
	public String tags;
	public String createdDate;
	public String itemName;
	public int entryType; // 0 = found, 1 = lost, 2 = donation
	public String description;

	/**
	 * simple constructor
	 * 
	 * @param creator
	 * @param itemName
	 * @param entryType
	 */
	public Item(String creator, String itemName, int entryType, String date,
			String category) {
		this.creator = creator;
		this.photo = "null";
		this.rewardAmt = 0;
		this.category = category;
		this.state = "a place";
		this.zipCode = "a place";
		this.streetAddress = "some street";
		this.tags = "";
		// DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		// this.createdDate = df.format(new Date());
		this.createdDate = date;
		this.itemName = itemName;
		this.entryType = entryType;
		this.description = "";
	}

	/**
	 * 
	 * @param creator
	 * @param rewardAmt
	 * @param category
	 * @param state
	 * @param zipCode
	 * @param streetAddress
	 * @param tags
	 * @param entryType
	 * @param itemName
	 * @param description
	 */
	public Item(String creator, int rewardAmt, String category, String state,
			String zipCode, String streetAddress, String tags, int entryType,
			String itemName, String description) {

		this.creator = creator;
		this.photo = "null";
		this.rewardAmt = rewardAmt;
		this.category = category;
		this.state = state;
		this.zipCode = zipCode;
		this.streetAddress = streetAddress;
		this.tags = tags;
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		this.createdDate = df.format(new Date());
		this.itemName = itemName;
		this.entryType = entryType;
		this.description = description;
	}
	
	

	/**
	 * makes an Item object
	 * 
	 * @param creator
	 *            the creator
	 * @param photo
	 *            the photo
	 * @param rewardAmt
	 *            the reward amount
	 * @param category
	 *            the category
	 * @param location
	 *            the location
	 * @param tags
	 *            tags
	 * @param isDonation
	 * @param createdDate
	 *            date created
	 * @param itemName
	 *            name
	 * @param isLost
	 * @param description
	 *            description
	 * @param state
	 *            state
	 * @param zipCode
	 *            zipcode
	 * @param streetAddress
	 *            street address
	 */
	public Item(String creator, String photo, int rewardAmt, String category,
			String location, String tags, String createdDate, String itemName,
			int entryType, String description, String state, String zipCode,
			String streetAddress, String country) {
		this.description = description;
		this.creator = creator;
		this.photo = photo;
		this.rewardAmt = rewardAmt;
		this.category = category;
		this.state = state;
		this.zipCode = zipCode;
		this.streetAddress = streetAddress;
		this.tags = tags;
		this.createdDate = createdDate;
		this.itemName = itemName;
		this.entryType = entryType;
		this.description = description;
		this.country = country;
	}
	
	/**
	 * makes an Item object
	 * 
	 * @param creator
	 *            the creator
	 * @param photo
	 *            the photo
	 * @param rewardAmt
	 *            the reward amount
	 * @param category
	 *            the category
	 * @param location
	 *            the location
	 * @param tags
	 *            tags
	 * @param isDonation
	 * @param createdDate
	 *            date created
	 * @param itemName
	 *            name
	 * @param isLost
	 * @param description
	 *            description
	 * @param state
	 *            state
	 * @param zipCode
	 *            zipcode
	 * @param streetAddress
	 *            street address
	 */
	public Item(String creator, int rewardAmt, String category,
			String tags, String createdDate, String itemName,
			int entryType, String description, String state, String zipCode,
			String streetAddress, String country) {
		this.description = description;
		this.creator = creator;
		this.photo = "null";
		this.rewardAmt = rewardAmt;
		this.category = category;
		this.state = state;
		this.zipCode = zipCode;
		this.streetAddress = streetAddress;
		this.tags = tags;
		this.createdDate = createdDate;
		this.itemName = itemName;
		this.entryType = entryType;
		this.description = description;
		this.country = country;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String status = "";
		if (entryType == 0)
			status = "Found";
		else if (entryType == 1)
			status = "Lost";
		else if (entryType == 2)
			status = "Donated";
		return  (itemName != null ? itemName + " " : "") + "\n"
				+ status + " by "
				+ (creator != null ?  creator + " " : "")
				+ "\n"
				+ "in " + (state != null ? state + " " : "")
				+ "on " +  (createdDate != null ? createdDate + " " : "");
				
				/*(creator != null ? "Creator: " + creator + ", " : "")
				+ "Reward: "
				+ rewardAmt
				+ ", "
				+ (category != null ? "Category: " + category + ", " : "")
				+ (state != null ? "State: " + state + ", " : "")
				+ (zipCode != null ? "Zipcode: " + zipCode + ", " : "")
				+ (streetAddress != null ? "Street Address: " + streetAddress + ", " : "")
				+ (tags != null ? "Tags: " + tags + ", " : "")
				+ (createdDate != null ? "Created Date: " + createdDate + ", " : "")
				+ (itemName != null ? "Name: " + itemName + ", " : "")
				+ "Status: " + status + ", "
				+ (description != null ? "Description: " + description : "");*/
	}

	public String getSortableDate() {
		String res = "";
		String[] parts;
		parts = createdDate.split("-");
		res = res + parts[2];
		if (parts[0].length() == 1) {
			parts[0] = "0" + parts[0];
		}
		res = res + parts[0];
		if (parts[1].length() == 1) {
			parts[1] = "0" + parts[1];
		}
		res = res + parts[1];
		return res;
	}

	public static class CompareByDate implements Comparator<Item> {
		public CompareByDate() {
		}

		public int compare(Item item1, Item item2) {
			return item1.getSortableDate().compareTo(item2.getSortableDate());
		}
	}

	public static class CompareByStatus implements Comparator<Item> {
		public CompareByStatus() {
		}

		public int compare(Item item1, Item item2) {
			return item1.entryType - item2.entryType;
		}
	}

	public static class CompareByCategory implements Comparator<Item> {
		public CompareByCategory() {
		}

		public int compare(Item item1, Item item2) {
			/*
			 * if (item1.category.equals("small")) { if
			 * (item2.category.equals("small")) return 0; return -1; } if
			 * (item1.category.equals("medium")) { if
			 * (item2.category.equals("small")) return 1; if
			 * (item2.category.equals("medium")) return 0; return -1; } else {
			 * if (item2.category.equals("large")) return 0; return 1;
			 */
			return item1.category.compareTo(item2.category);
		}
	}
}
